##########################################################
# John Henderson and Alex Theodoridis
# Replication Data for: "Seeing Spots", 
#  Forthcoming in Political Behavior, August 20, 2017
# 
##########################################################
#
#  table_ix.R
#  -- file reproduces regression results for tables ix in the appendix
#
##########################################################
    
rm(list=ls())  
set.seed(1005)                                  
source('~/Dropbox/Seeing_Spots/replication/covar_data_vagov.R')  # contains pre_data

ymat=cbind(video_skipped, video_skipped_alt,replay, share, getlink, all_y,time_watched, total_time)   
                        
# SUR for coherency interpretations of coefficients across all outcomes (alt. additive outcome)
library(systemfit)
ymat[,1]=1-ymat[,1]

########################################################### 
# experimental regression results + observational controls
########################################################## 
  
party_tr=as.numeric(dem_ad)
party_tr[which(party_tr==1)]=-1
party_tr[which(party_tr==0)]=1

tone_tr=as.numeric(pos_ad)
tone_tr[which(tone_tr==1)]=1
tone_tr[which(tone_tr==0)]=-1

# some interactions and additional variables for below               
news_tone=newsint*tone_tr
women_tone=women*tone_tr 

va_data$pid7_pre=as.numeric(va_data$pid7_pre)-4                                            
va_data$pid7_pre[which(va_data$pid7_pre==4)]=0
pid_strenth=abs(va_data$pid7_pre)    
                                    
va_data$uncertain=as.numeric(va_data$govhypo_pre_pre=="I'm not sure ")  

###################
# TABLE IX - baseline ols va

r1 <- ymat[,1]~
party_tr*pid_lean*tone_tr+
newsint+pid_strenth+  
reg+uncertain+battleground+
abs(mdsscale)+#approval+
women+black+educ+income+married+
unemployed+#union+
vote+churchattend+news_tone
	
r2 <- ymat[,3]~
party_tr*pid_lean*tone_tr+
newsint+pid_strenth+  
reg+uncertain+battleground+
abs(mdsscale)+#approval+
women+black+educ+income+married+
unemployed+#union+
vote+churchattend+news_tone

r3 <- ymat[,4]~
party_tr*pid_lean*tone_tr+
newsint+pid_strenth+  
reg+uncertain+battleground+
abs(mdsscale)+#approval+
women+black+educ+income+married+
unemployed+#union+
vote+churchattend+news_tone

r4 <- ymat[,5]~
party_tr*pid_lean*tone_tr+
newsint+pid_strenth+  
reg+uncertain+battleground+
abs(mdsscale)+#approval+
women+black+educ+income+married+
unemployed+#union+
vote+churchattend+news_tone

fitsur <- systemfit(list(skipreg = r1, replayreg = r2, sharereg = r3, linkreg = r4),method='SUR')
summary(fitsur)  

fitols <- systemfit(list(skipreg = r1, replayreg = r2, sharereg = r3, linkreg = r4),method='OLS')
summary(fitols)

library(car)
# joint F over SUR
lst=rownames(summary(lm(r4))$coef)   
joint_p=as.data.frame(matrix(1,length(lst),1))

for(j in 1:nrow(joint_p)){ 
	restriction <- c(
		paste("skipreg_",lst[j],"=0",sep=''),
		paste("replayreg_",lst[j],"=0",sep=''),
		paste("sharereg_",lst[j],"=0",sep=''),
		paste("linkreg_",lst[j],"=0",sep=''))
	x=linearHypothesis(fitols, hypothesis.matrix=restriction, test = "F")
	joint_p[j,1]=(x[[4]])[2] 
	rownames(joint_p)[j]=lst[j]
}
 
t(t(summary(fitsur)$coefficients[,1]))
t(t(summary(fitsur)$coefficients[,2]))
t(t(summary(fitols)$coefficients[,1]))
t(t(summary(fitols)$coefficients[,2]))
round(joint_p,digits=3)
           
###################
# TABLE III - logit     

log1=summary(glm(r1,family=binomial(link=logit)))
log2=summary(glm(r2,family=binomial(link=logit)))
log3=summary(glm(r3,family=binomial(link=logit)))
log4=summary(glm(r4,family=binomial(link=logit))) 
  

#END #END table_ix.R